package com.delayqueue.common.netty;

import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;

import java.net.InetSocketAddress;

public class NettyUtil {

    public static Address getRemoteAddress(ChannelHandlerContext context) {
        return getRemoteAddress(context.channel());
    }

    public static Address getRemoteAddress(Channel channel) {
        InetSocketAddress address = (InetSocketAddress) channel.remoteAddress();
        return new Address(address.getAddress().getHostAddress(), address.getPort());
    }

    public static Address getLocalAddress(ChannelHandlerContext context) {
        return getLocalAddress(context.channel());
    }

    public static Address getLocalAddress(Channel channel) {
        InetSocketAddress address = (InetSocketAddress) channel.localAddress();
        return new Address(address.getAddress().getHostAddress(), address.getPort());
    }
}
